/*
 * Copyright TLR Team, Inc. All Rights Reserved.
 */
package com.tlr.device.rest;

import com.tfb.rong.Device;
import com.tfb.service.UserAssessmentI;
import static com.tfb.service.utility.HttpUtil.getUserDBID;
import com.tlr.device.model.AirPurifier;
import com.tlr.device.model.Result;
import com.tlr.device.service.DeviceDatabaseService;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

@Path("/air_purifier")
public class AirPurifierResource extends AirDeviceResource {

    private static final Logger log = Logger.getLogger(AirPurifierResource.class);
    @Context
    ServletContext context;

    @Override
    protected synchronized void initDatabaseService() {
        if (deviceService == null || userService == null) {
            ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(context);
            if (deviceService == null) {
                deviceService = (DeviceDatabaseService) ac.getBean("deviceDatabaseService");
            }
            if (userService == null) {
                userService = (UserAssessmentI) ac.getBean("userAssessmentService");
            }
        }
    }

    @Path("/edit_bind")
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Result<String> editBindDevice(@Context HttpServletRequest request, String message) {
        return super.doEditBindDevice(request, message);
    }

    @Path("/add_bind")
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Result<String> bindDevice(@Context HttpServletRequest request, String message) {
        return super.doBindDevice(request, message, "空气净化器", 4);
    }

    @Path("binded")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Result<List<AirPurifier>> getBindedAirDevices(@Context HttpServletRequest request) {
        try {
            // Get all active air devices for map_info
            initDatabaseService();
            String userID = getUserDBID(request);
            List<Device> devices = deviceService.getDeviceMapper().selectDeviceWithBindedInfoByUserIDAndDeviceType(userID, 4);
            List<AirPurifier> purifiers = new ArrayList<>();
            for (Device device : devices) {
                AirPurifier purifier = new AirPurifier();
                purifier.setDevice_id(device.getDevicemac());
                purifier.setStorage_id(device.getDeviceid());
                purifier.setNick_name(device.getBindNickName());
                purifier.setStatus(device.getStatus());
                purifier.setStatus_label("正常");
                purifiers.add(purifier);
            }
            return new Result<>(0, "OK", purifiers);
        } catch (Exception e) {
            log.error(e.fillInStackTrace());
            return new Result<>(500, "获取用户空气净化器列表失败");
        }
    }

    @Path("/update_position")
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Override
    public Result<String> updatePosition(String message) {
        return super.updatePosition(message);
    }
}
